package com.liu.web;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

@ComponentScan("com.liu")
public class WebApp implements WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        //spring mvc工厂类 读取 注解配置
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        //注册bean 组件类
        context.register(WebApp.class);

        //定义前端控制器
        DispatcherServlet dispatcherServlet = new DispatcherServlet(context);
        //动态注册servlet
        ServletRegistration.Dynamic registration = servletContext.addServlet("dispatcherServlet",dispatcherServlet);
        //容器启动时初始化
        registration.setLoadOnStartup(1);
        //映射路径
        registration.addMapping("/");

    }
}
